package com.example.xzc.cainiaoshangcheng.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;

import com.example.xzc.cainiaoshangcheng.R;
import com.example.xzc.cainiaoshangcheng.Adapter.TestAdapter;
import com.example.xzc.cainiaoshangcheng.widget.FullyLinearLayoutManager;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;



/**
 * @projectName: CaiNiaoShangCheng    ---------> 项目名称
 * @description: --------->SwipeRefreshLayout使用
 * @author: xzc                              --------->作者
 * @date: 2017-03-14    --------->日期
 * @time: 11:03               --------->时间
 */

public class Test2Activity extends AppCompatActivity {

    @ViewInject(R.id.rv_test2)
    private RecyclerView rv_test2;

    @ViewInject(R.id.sr_test2)
    private SwipeRefreshLayout sLayout;

    private TestAdapter myAdapter;
    private List<String> datas;
    private List<String> temp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);

        x.view().inject(this);

        initData();
        initView();
        initSwipeRefreshLayout();
    }

    private void initView() {
        rv_test2.setLayoutManager(new FullyLinearLayoutManager(this,FullyLinearLayoutManager.VERTICAL, false));
        myAdapter = new TestAdapter(datas,this);
        rv_test2.setAdapter(myAdapter);

    }

    private void initSwipeRefreshLayout() {
        sLayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_green_light,
                android.R.color.holo_red_light);

        sLayout.setDistanceToTriggerSync(100);

        sLayout.setSize(SwipeRefreshLayout.LARGE);

        sLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        temp.clear();
                        temp.addAll(datas);
                        datas.clear();
                        for(int i=0; i<10; i++){
                            datas.add("more"+i);
                        }
                        datas.addAll(temp);
                        myAdapter.notifyDataSetChanged();

                        rv_test2.scrollToPosition(0);

                        sLayout.setRefreshing(false);
                    }
                },3000);
            }
        });



    }

    private void initData() {
        datas = new ArrayList<>();
        temp = new ArrayList<>();
        for(int i=0; i<50; i++){
            datas.add("test"+i);
        }
    }
}
